﻿using UnityEngine;
using System.Collections;

[RequireComponent(typeof(BoxCollider2D))]
[RequireComponent(typeof(InteractiveMenu))]
public class InteractiveObject : MonoBehaviour, IInteractiveObject {

	public Transform actionTrigger;
	public string Description;
	public GameObject MenuPosition;

	[HideInInspector]
	public InteractiveMenu actionMenu;

	bool showingMenu;

	void Awake() {
		OnAwake();
	}

	public void OnAwake() {
		BoxCollider2D boxCollider = GetComponent<BoxCollider2D>();
		boxCollider.isTrigger = true;
		actionMenu = GetComponent<InteractiveMenu>();
		actionMenu.enabled = false;
	
		showingMenu = false;

		if (MenuPosition == null) {
			MenuPosition = new GameObject();
			MenuPosition.transform.position = this.transform.position;
		}
	}

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	void OnTriggerEnter2D(Collider2D other) {
		if (other.transform == actionTrigger && other.GetType() == typeof(BoxCollider2D)) {
			showingMenu = !showingMenu;
			if (showingMenu) {
				actionMenu.SetPositionReference(MenuPosition.transform.position);
				actionMenu.enabled = true;
			}
		}
	}

	void OnTriggerExit2D(Collider2D other) {
		if (other.transform == actionTrigger && other.GetType() == typeof(BoxCollider2D)) {
			if (showingMenu) {
				actionMenu.enabled = false;
				actionMenu.ShowingActions = false;
				actionMenu.StopAllActions();
				showingMenu = false;
			}
		}
	}


	public virtual ArrayList GetActions() 
	{
		Inspect inspectAction = (Inspect)this.gameObject.AddComponent(typeof(Inspect));
		inspectAction.Description = this.Description;
		ArrayList actions = new ArrayList();
		actions.Add(inspectAction);

		return actions;
	}


}
